﻿/*
 * KLF Headless Browser
 * Written by Kris Oye <kristianoye@gmail.com>
 * Copyright (C) 2011-2012.  All rights reserved.
 */
namespace KLF.Lib.Browser.DOM.HTML
{
    /// <summary>
    /// Provides the form property for form-related elements.
    /// </summary>
    abstract public class HTMLFormElementBase : HTMLElement
    {
        #region Constructors

        internal HTMLFormElementBase(Document owner, string tagName)
            : base(owner, tagName)
        {
        }

        #endregion

        #region DOM Level 2 HTML Non-Standard Methods

        /// <summary>
        /// Returns the FORM element containing this control. Returns null
        /// if this control is not within the context of a form.
        /// </summary>
        public HTMLFormElement form
        {
            get
            {
                Node p = Parent;
                do
                {
                    if (p != null)
                    {
                        if (p is HTMLFormElement)
                            return (HTMLFormElement)p;
                        else
                            p = p.Parent;

                        if (p is Document)
                            p = null;
                    }
                }
                while (p != null);

                return null;
            }
        }

        #endregion
    }
}
